package com.ht.medicaltreatment.view

import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import com.blankj.utilcode.util.ToastUtils
import com.ht.medicaltreatment.R
import com.xgst.lib.utils.ScreenUtils
import kotlinx.android.synthetic.main.dialog_input_count_layout.etContent
import kotlinx.android.synthetic.main.dialog_input_count_layout.tvCancel
import kotlinx.android.synthetic.main.dialog_input_count_layout.tvSubmit

//输入数量
class InputCountDialog2(context: Context, number:Int,useNumber:Int) : Dialog(context, R.style.dialogStyle) {

    private lateinit var mClick:(Int) -> Unit

    init {
        val view =
            LayoutInflater.from(context).inflate(R.layout.dialog_input_count_layout, null, false)
        setContentView(view)
        val attributes = window!!.attributes
        attributes.width= ScreenUtils.dp2px(context,300)
        //attributes.height= ScreenUtils.dp2px(context,300)
        window!!.attributes = attributes
        setCancelable(true)
        etContent.setText("$useNumber")
        tvSubmit.setOnClickListener {
            if(TextUtils.isEmpty(etContent.text.toString())){
                ToastUtils.showShort("请输入数量")
                return@setOnClickListener
            }
            val count = etContent.text.toString().toInt()
            if(count <= 0){
                ToastUtils.showShort("最少申请1个")
                return@setOnClickListener
            }
            if(count > number){
                ToastUtils.showShort("最多可以申请$number 个")
                return@setOnClickListener
            }
            mClick(count)
            dismiss()
        }
        tvCancel.setOnClickListener {
            dismiss()
        }
    }

    fun showDialog(onClick:(Int)->Unit){
        mClick = onClick
        show()
    }
}